package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1.market;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.net_marketing.cloud.business.dao.db.UserPayGoodsOrder;
import com.gzhryc.net_marketing.cloud.business.services.UserPayGoodsOrderService;
import com.gzhryc.net_marketing.cloud.business.services.dto.UserPayGoodsOrderSearch;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.market.GoodsOrderListRequest;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.market.GoodsOrderListResponse;

public class GoodsOrderController {

    @Route(value = "/agent_market/api/v1/order/list.data")
    public GoodsOrderListResponse listData(HttpServletRequest request) {
        GoodsOrderListResponse apiResp = new GoodsOrderListResponse(ClientAPICodeConstants.NULL_DATA,"暂无数据");
        GoodsOrderListRequest apiReq = WebUtils.getJsonRequest(request, GoodsOrderListRequest.class);
        if (apiReq != null) {
            LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
            if (loginUser != null && loginUser.getId() != null) {
                Integer pageNo = apiReq.getBody().getPageNo() == null ? 1 : apiReq.getBody().getPageNo();

                UserPayGoodsOrderSearch search = new UserPayGoodsOrderSearch();
                search.setUserId(loginUser.getId());
                search.setTradeNo(apiReq.getBody().getTrim());
                search.setState(apiReq.getBody().getState());
                List<UserPayGoodsOrder> orderList = UserPayGoodsOrderService.self().findBySearch(search, pageNo, WebConstants.PAGE_LIMIT);

                if(orderList != null && orderList.size() > 0){
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                    apiResp.getBody().setItems(new ArrayList<>());
                    for(UserPayGoodsOrder order : orderList){
                        GoodsOrderListResponse.Item item = new GoodsOrderListResponse.Item();
                        item.setTradeNo(order.getTradeNo());
                        item.setGoodsId(order.getGoodsId());
                        item.setGoodsName(order.getGoodsName());
                        item.setGoodsSpaceId(order.getGoodsSpaceId());
                        item.setGoodsSpaceName(order.getGoodsSpaceName());
                        item.setGoodsImg(order.getGoodsImg());
                        item.setGoodsCount(order.getGoodsCount());
                        item.setGoodsMoneyStr(order.getGoodsMoneyStr());
                        item.setMoneyStr(order.getMoneyStr());
                        item.setLogisticsMoneyStr(order.getLogisticsMoneyStr());
                        item.setState(order.getState());
                        item.setStateName(order.getStateName());
                        item.setCreateDate(order.getCreateDate());
                        apiResp.getBody().getItems().add(item);
                    }
                }
            }
        }
        return apiResp;
    }

}
